package handlers

import (
	"github.com/gin-gonic/gin"
	"store-master/models"
	"strconv"
)

func StoreAdd(c *gin.Context) {
	store_name := c.PostForm("store_name")
	store_address := c.PostForm("store_address")
	store_tel := c.PostForm("store_tel")

	data := models.Store{
		StoreName:    store_name,
		StoreAddress: store_address,
		StoreTel:     store_tel,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func StoreList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))

	Offset := (page - 1) * size

	var store []models.Store
	models.DB.Offset(Offset).Limit(size).Debug().Find(&store)

	var total int64
	models.DB.Model(&models.Store{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  store,
		"total": total,
	})
	return
}
